package com.atguigu.gmall.common.utils;

import com.atguigu.gmall.common.auth.UserAuthInfo;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

public class UserAuthUtils {

    public static UserAuthInfo getUserAuthInfo(){

        // 数据封装
        UserAuthInfo userAuthInfo = new UserAuthInfo() ;

        // 从RequestContextHolder中获取ServletRequestAttributes对象，ServletRequestAttributes封装HttpServletRequest对象
        ServletRequestAttributes servletRequestAttributes =  (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if(servletRequestAttributes != null) {
            HttpServletRequest httpServletRequest = servletRequestAttributes.getRequest();
            String userId = httpServletRequest.getHeader("userId");
            String userTempId = httpServletRequest.getHeader("userTempId");
            userAuthInfo.setUserId(userId);
            userAuthInfo.setUserTempId(userTempId);
        }

        return userAuthInfo ;

    }

}
